package org.zhq.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zhq.Const;
import org.zhq.ServerResult;
import org.zhq.pojo.Users;
import org.zhq.pojo.bo.UserBO;
import org.zhq.pojo.vo.UsersVO;
import org.zhq.service.ShopCartService;
import org.zhq.service.UsersService;
import org.zhq.utils.CookieUtils;
import org.zhq.utils.MD5Utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "注册登录",tags = {"用于注册登录的相关接口"})
@RestController
@RequestMapping("passport")
public class PassportController {

    private final UsersService usersService;
    private final ShopCartService shopCartService;

    @Autowired
    public PassportController(UsersService usersService, ShopCartService shopCartService) {
        this.usersService = usersService;
        this.shopCartService = shopCartService;
    }

    @ApiOperation(value = "用户名是否存在",notes = "用户名是否存在",httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public ServerResult usernameIsExist(@RequestParam String username){
        //0.判断用户名不能为空
        if(StrUtil.isBlank(username)){
            return ServerResult.errorMsg("用户名不能为空");
        }
        //1.查找注册的用户名是否存在
        boolean existed = usersService.queryUsernameIsExist(username);
        if(existed){
            return ServerResult.errorMsg("用户名已经存在");
        }
        //2.请求成功.用户名没重复
        return ServerResult.ok();
    }

    @ApiOperation(value = "注册",notes = "注册",httpMethod = "POST")
    @PostMapping("/regist")
    public ServerResult regist(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response){
        String username = userBO.getUsername();
        String password = userBO.getPassword();
        String confirmPassword = userBO.getConfirmPassword();
        //0.判断用户名和密码不能为空
        if(StrUtil.isBlank(username)||
            StrUtil.isBlank(password)||
            StrUtil.isBlank(confirmPassword)){
            return ServerResult.errorMsg("用户名密码不能为空");
        }
        //1.密码长度不能少于6位
        if(password.length()<6){
            return ServerResult.errorMsg("密码不能少于6位");
        }
        //2.判断两次密码是否一致
        if (!StrUtil.equals(password, confirmPassword)) {
            return ServerResult.errorMsg("两次密码不一致");
        }
        //3.查询用户名是否存在
        boolean existed = usersService.queryUsernameIsExist(username);
        if (existed) {
            return ServerResult.errorMsg("用户名已经存在");
        }
        //4.实现注册
        Users users = usersService.createUser(userBO);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users, usersVO);
        // 生产用户token，存入redis会话
        usersService.storeUserForLogin(usersVO, request, response);
        //同步购物车数据
        shopCartService.syncShopCart(users.getId(), request, response);
        return ServerResult.ok();
    }

    @ApiOperation(value = "登录", notes = "登录", httpMethod = "POST")
    @PostMapping("/login")
    public ServerResult login(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response) {
        String username = userBO.getUsername();
        String password = userBO.getPassword();
        //0.判断用户名和密码不能为空
        if (StrUtil.isBlank(username) ||
                StrUtil.isBlank(password)) {
            return ServerResult.errorMsg("用户名密码不能为空");
        }
        //1.实现登录
        Users users = usersService.queryUserForLogin(username, MD5Utils.getMD5Str(password));
        if (users == null) {
            return ServerResult.errorMsg("用户名密码不正确");
        }
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users, usersVO);
        //生产用户token，存入redis会话
        usersService.storeUserForLogin(usersVO, request, response);
        //同步购物车数据
        shopCartService.syncShopCart(users.getId(), request, response);
        return ServerResult.ok(users);
    }


    @ApiOperation(value = "退出", notes = "退出", httpMethod = "POST")
    @PostMapping("/logout")
    public ServerResult logout(@RequestParam String userId, HttpServletRequest request, HttpServletResponse response) {
        CookieUtils.deleteCookie(request, response, Const.CURRENT_USER);
        //用户退出登录,清空购物车
        shopCartService.cleanShopCart(userId, request, response);
        //分布式会话中清除用户数据
        usersService.removeUserForLogin(userId, request, response);
        return ServerResult.ok();
    }

}
